/*
 * @Author: szx
 * @Date: 2022-03-02 14:30:58
 * @LastEditTime: 2022-03-02 14:39:50
 * @Description:
 * @FilePath: \leetcode\100-199\144\144_2.js
 */
function TreeNode(val, left, right) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
}

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var preorderTraversal = function (root) {
    if(!root) return[];
    const st = [root];
    const res = [];
    while (st.length) {
        const node = st.pop();
        res.push(node.val);
        if (node.right) st.push(node.right);
        if (node.left) st.push(node.left);
    }
    return res;
};
